package com.huawei.esdk.tp.device.smc.convert;

import java.util.ArrayList;
import java.util.List;

import javax.xml.datatype.XMLGregorianCalendar;

import com.huawei.esdk.platform.common.SDKErrorCode;
import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.utils.DateUtils;
import com.huawei.esdk.tp.cxf.enumgen.FilterColumn2;
import com.huawei.esdk.tp.cxf.enumgen.SortType;
import com.huawei.esdk.tp.domain.model.Site;
import com.huawei.esdk.tp.domain.model.bean.DateTimeRange;
import com.huawei.esdk.tp.domain.model.bean.FreeBusyInfo;
import com.huawei.esdk.tp.domain.model.bean.PagedList;
import com.huawei.esdk.tp.domain.model.bean.QueryCondition;
import com.huawei.esdk.tp.domain.model.bean.QuerySites;
import com.huawei.esdk.tp.domain.model.bean.SiteFreeBusyRes;
import com.huawei.esdk.tp.domain.model.bean.SortField;
import com.huawei.esdk.tp.southcommu.cxf.gen.ArrayOfFilterBaseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ArrayOfSortItemSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.ArrayOfStringSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DateTimeFilterSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DateTimeRangeSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.DoubleFilterSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.FilterBaseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.FreeBusy2South;
import com.huawei.esdk.tp.southcommu.cxf.gen.IPAddressExSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.IPAddressFilterSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.IntFilterSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.PageParamSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.PagesInfoSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QueryConfigSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuerySiteFreeBusyResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuerySiteFreeBusySouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuerySitesResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.QuerySitesSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SiteFreeBusyStateEx2South;
import com.huawei.esdk.tp.southcommu.cxf.gen.SiteParamExSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SortItemSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.StringFilterSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SynchSiteFreeBusyResponseSouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.SynchSiteFreeBusySouth;
import com.huawei.esdk.tp.southcommu.cxf.gen.TimeSpanExSouth;

/**
 * * @author t00212088 * *
 */
public class TPSiteMgrConvert
{
    public QuerySitesSouth querySites2QuerySitesSouth(QuerySites query)
    {
        QuerySitesSouth south = new QuerySitesSouth();
        QueryConfigSouth config = new QueryConfigSouth();
        // 排序参数
        ArrayOfSortItemSouth sortItem = new ArrayOfSortItemSouth();
         if (null != query.getSortField())
         {
             //做排序用 
             for(SortField item : query.getSortField())
             {
                 SortItemSouth sortItemSoap = new SortItemSouth();
                 sortItemSoap.setSort(0 == item.getIsAscend() ? SortType.ASC : SortType.DESC);
                 sortItemSoap.setItemIndex(FilterColumn2Int2Enum(item.getFieldIndex()));
                 sortItem.getSortItem().add(sortItemSoap);
             }
         }
        config.setSortItems(sortItem);
        // 过滤参数
        ArrayOfFilterBaseSouth filter = new ArrayOfFilterBaseSouth();
         if (null != query.getQueryCondition())
         {
             //做过滤用
             for(QueryCondition item : query.getQueryCondition())
             {
                 FilterBaseSouth fielter = getFilterModal2Soap(item);
                 if(null != fielter)
                 {
                     filter.getFilterBase().add(fielter);
                 }
             }
         }
        config.setFilters(filter);
        // 焦点数据
        config.setFocusItem(query.getFocusItem());
        // 分页数据'
        PageParamSouth page = new PageParamSouth();
        page.setCurrentPage(query.getCurrentPage());
        // 设定每页数为会场总数，一次性查出
        page.setNumberPerPage(query.getNumberPerPage());
        config.setPageParam(page);

        south.setQueryConfig(config);
        return south;
    }

    public SDKResult<PagedList<Site>> querySitesResponseSouth2SDKResult(
            QuerySitesResponseSouth southResponse)
    {
        SDKResult<PagedList<Site>> result = new SDKResult<PagedList<Site>>();
        PagedList<Site> pagedList = new PagedList<Site>();
        if (null == southResponse)
        {
            result.setResult(pagedList);
            return result;
        }

        // 书写返回值
        result.setErrCode(southResponse.getQuerySitesResult().getErrCode());
        result.setSubSystem(String.valueOf(southResponse.getQuerySitesResult()
                .getSubSystem()));
        result.setDescription(southResponse.getQuerySitesResult()
                .getDescription());
        if (null != southResponse.getSites()
                && southResponse.getSites().getSiteParamEx().size() > 0)
        {
            List<SiteParamExSouth> siteParamEx = southResponse.getSites()
                    .getSiteParamEx();
            Site site = null;
            for (int i = 0; i < siteParamEx.size(); i++)
            {
                site = new Site();
                site.setUri(siteParamEx.get(i).getUri());
                site.setName(siteParamEx.get(i).getName());
                site.setSiteType(siteParamEx.get(i).getType().toString());
                pagedList.addRecord(site);
            }
        }
        result.setResult(pagedList);

        PagesInfoSouth pageInfo = southResponse.getPageInfo();
        if(null != pageInfo)
        {
            pagedList.setCurPageNo(pageInfo.getCurrentPage());
            pagedList.setTotalPageCount(pageInfo.getTotalPages());
            pagedList.setTotalRecordCount(pageInfo.getTotalRecords());
        }

        result.setResult(pagedList);
        
        return result;
    }

    public QuerySiteFreeBusySouth querySiteFreeBusy2QuerySiteFreeBusySouth(
            List<String> siteUris, DateTimeRange timeRange)
    {
        QuerySiteFreeBusySouth reqBean = new QuerySiteFreeBusySouth();
        // 添加多个会场URI
        ArrayOfStringSouth arrayofString = new ArrayOfStringSouth();
        arrayofString.getString().addAll(siteUris);
        reqBean.setSiteUris(arrayofString);
        // 指定时间范围
        DateTimeRangeSouth timeRangeSouth = new DateTimeRangeSouth();
        timeRangeSouth.setStart(DateUtils.toGregorianCalendarDateSMC(timeRange
                .getStart()));
        TimeSpanExSouth timeSpan = new TimeSpanExSouth();
        timeSpan.setTimeSpan(timeRange.getSpan());
        timeRangeSouth.setSpan(timeSpan);
        reqBean.setTimeRange(timeRangeSouth);
        return reqBean;
    }

    public SiteFreeBusyRes querySiteFreeBusyResponseSouth2SDKResult(
            QuerySiteFreeBusyResponseSouth resBean)
    {
        SiteFreeBusyRes siteFreeBusyRes = new SiteFreeBusyRes();
        SDKErrorCode result = new SDKErrorCode();
        result.setErrCode(resBean.getQuerySiteFreeBusyResult().getErrCode());
        siteFreeBusyRes.setResult(result);
        if (0 == siteFreeBusyRes.getResult().getErrCode()
                && null != resBean.getFreebusy()
                && null != resBean.getFreebusy().getSiteFreeBusyStateEx2())
        {
            List<Site> sites = new ArrayList<Site>();
            Site site = null;
            List<SiteFreeBusyStateEx2South> siteFreeBusyStateList = resBean
                    .getFreebusy().getSiteFreeBusyStateEx2();
            for (SiteFreeBusyStateEx2South siteFreeBusyState : siteFreeBusyStateList)
            {
                site = new Site();
                // 会场URI
                site.setUri(siteFreeBusyState.getSiteUri());
                // 会场忙闲状态
                List<FreeBusyInfo> freeBusyInfos = new ArrayList<FreeBusyInfo>();
                FreeBusyInfo freeBusyInfo = null;
                for (FreeBusy2South freeBusy : siteFreeBusyState.getFreebusys()
                        .getFreeBusy2())
                {
                    freeBusyInfo = new FreeBusyInfo();
                    freeBusyInfo.setConfExternalId(freeBusy.getConfId()
                            .getConfExternalId());
                    freeBusyInfo.setConfInternalId(freeBusy.getConfId()
                            .getConfInternalId().toString());
                    freeBusyInfo.setConfName(freeBusy.getConfName());
                    freeBusyInfo.setFreeBusyState(freeBusy.getState()
                            .toString());
                    DateTimeRange tRange = new DateTimeRange();
                    tRange.setStart(DateUtils.toDate(freeBusy.getTimeRange()
                            .getStart()));
                    tRange.setSpan(freeBusy.getTimeRange().getSpan()
                            .getTimeSpan());
                    freeBusyInfo.setTimeRange(tRange);
                    // 添加一个该会场所在的会议的信息
                    freeBusyInfos.add(freeBusyInfo);
                }
                site.setFreeBusyInfoList(freeBusyInfos);
                // 添加 一个查询到的会场
                sites.add(site);
            }

            siteFreeBusyRes.setSiteFreeBusyStateList(sites);
        }
        return siteFreeBusyRes;
    }

    public SynchSiteFreeBusySouth synchSiteFreeBusy2SynchSiteFreeBusySouth(
            List<String> siteUris, DateTimeRange timeRange)
    {
        SynchSiteFreeBusySouth reqBean = new SynchSiteFreeBusySouth();
        // 添加多个会场URI
        ArrayOfStringSouth arrayofString = new ArrayOfStringSouth();
        arrayofString.getString().addAll(siteUris);
        reqBean.setSiteUris(arrayofString);
        // 指定时间范围
        DateTimeRangeSouth timeRangeSouth = new DateTimeRangeSouth();
        timeRangeSouth.setStart(DateUtils.toGregorianCalendarDateSMC(timeRange
                .getStart()));
        TimeSpanExSouth timeSpan = new TimeSpanExSouth();
        timeSpan.setTimeSpan(timeRange.getSpan());
        timeRangeSouth.setSpan(timeSpan);
        reqBean.setTimeRange(timeRangeSouth);
        return reqBean;
    }

    public SiteFreeBusyRes synchSiteFreeBusyResponseSouth2SDKResult(
            SynchSiteFreeBusyResponseSouth resBean)
    {
        SiteFreeBusyRes siteFreeBusyRes = new SiteFreeBusyRes();
        SDKErrorCode result = new SDKErrorCode();
        result.setErrCode(resBean.getSynchSiteFreeBusyResult().getErrCode());
        siteFreeBusyRes.setResult(result);
        if (0 == siteFreeBusyRes.getResult().getErrCode()
                && null != resBean.getFreebusy()
                && null != resBean.getFreebusy().getSiteFreeBusyStateEx2())
        {
            List<Site> sites = new ArrayList<Site>();
            Site site = null;
            List<SiteFreeBusyStateEx2South> siteFreeBusyStateList = resBean
                    .getFreebusy().getSiteFreeBusyStateEx2();
            for (SiteFreeBusyStateEx2South siteFreeBusyState : siteFreeBusyStateList)
            {
                site = new Site();
                // 会场URI
                site.setUri(siteFreeBusyState.getSiteUri());
                // 会场忙闲状态
                List<FreeBusyInfo> freeBusyInfos = new ArrayList<FreeBusyInfo>();
                FreeBusyInfo freeBusyInfo = null;
                for (FreeBusy2South freeBusy : siteFreeBusyState.getFreebusys()
                        .getFreeBusy2())
                {
                    freeBusyInfo = new FreeBusyInfo();
                    freeBusyInfo.setConfExternalId(freeBusy.getConfId()
                            .getConfExternalId());
                    freeBusyInfo.setConfInternalId(freeBusy.getConfId()
                            .getConfInternalId().toString());
                    freeBusyInfo.setConfName(freeBusy.getConfName());
                    freeBusyInfo.setFreeBusyState(freeBusy.getState()
                            .toString());
                    DateTimeRange tRange = new DateTimeRange();
                    tRange.setStart(DateUtils.toDate(freeBusy.getTimeRange()
                            .getStart()));
                    tRange.setSpan(freeBusy.getTimeRange().getSpan()
                            .getTimeSpan());
                    freeBusyInfo.setTimeRange(tRange);
                    // 添加一个该会场所在的会议的信息
                    freeBusyInfos.add(freeBusyInfo);
                }
                site.setFreeBusyInfoList(freeBusyInfos);
                // 添加 一个查询到的会场
                sites.add(site);
            }

            siteFreeBusyRes.setSiteFreeBusyStateList(sites);
        }
        return siteFreeBusyRes;
    }
    
    private FilterColumn2 FilterColumn2Int2Enum(Integer val)
    {
        switch(val){
            case 0 : return FilterColumn2.SITE_NAME;
            case 1 : return FilterColumn2.SITE_URI;
            case 2 : return FilterColumn2.SITE_STATUS;
            case 3 : return FilterColumn2.SITE_TYPE;
            case 4 : return FilterColumn2.CONF_TEMPLATE_NAME;
            case 5 : return FilterColumn2.CONF_TEMPLATE_ACCESS_CODE;
            default : return null;
        }
        
    }
    
    private FilterBaseSouth getFilterModal2Soap(QueryCondition item)
    {

        if(null == item)
        {
            return null;
        }
        if("1".equals(item.getConditionType()))
        {
            if(2 == item.getConditionValue().length)
            {
                StringFilterSouth stringFilter = new StringFilterSouth();
                stringFilter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                stringFilter.setValue((String)item.getConditionValue()[1]);
                return stringFilter;
            }
            else
            {
                return null;
            }
        }
        else if("2".equals(item.getConditionType()))
        {
            if(4 == item.getConditionValue().length)
            {
                IntFilterSouth filter = new IntFilterSouth();
                filter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((Integer)item.getConditionValue()[1]);
                filter.setLessThanValue((Integer)item.getConditionValue()[2]);
                filter.setEqualToValue((Integer)item.getConditionValue()[3]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if("3".equals(item.getConditionType()))
        {
            if(4 == item.getConditionValue().length)
            {
                DoubleFilterSouth filter = new DoubleFilterSouth();
                filter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((Double)item.getConditionValue()[1]);
                filter.setLessThanValue((Double)item.getConditionValue()[2]);
                filter.setEqualToValue((Double)item.getConditionValue()[3]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if("4".equals(item.getConditionType()))
        {
            if(3 == item.getConditionValue().length)
            {
                DateTimeFilterSouth filter = new DateTimeFilterSouth();
                filter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                filter.setGreaterThanValue((XMLGregorianCalendar)item.getConditionValue()[1]);
                filter.setLessThanValue((XMLGregorianCalendar)item.getConditionValue()[2]);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else if("5".equals(item.getConditionType()))
        {
            if(2 == item.getConditionValue().length)
            {
                IPAddressFilterSouth filter = new IPAddressFilterSouth();
                filter.setColumnIndex(FilterColumn2Int2Enum((Integer)item.getConditionValue()[0]));
                IPAddressExSouth value = new IPAddressExSouth();
                value.setIPAddress((String)item.getConditionValue()[0]);
                filter.setValue(value);
                return filter;
            }
            else
            {
                return null;
            }
        }
        else
        {
            return null;
        }

    }
}
